#!/bin/bash

# 设置阈值
cpu_alert=80
mem_alert=80
disk_alert=80
net_alert=80

# 获取CPU和内存利用率
cpu_usage=$(top -b -n1 | grep "Cpu(s)" | awk '{print $2 + $4}')
mem_usage=$(free -m | awk 'NR==2{printf "%.2f\t", $3*100/$2 }')

# 获取磁盘利用率
disk_usage=$(df -h | awk '$NF=="/"{printf "%.2f\t", $5}' | cut -d'%' -f1)

# 获取网络利用率
net_in=$(cat /proc/net/dev | grep 'eth0:' | awk '{print $2}' | cut -d':' -f2)
net_out=$(cat /proc/net/dev | grep 'eth0:' | awk '{print $10}' | cut -d':' -f2)
net_usage=$(expr $net_in + $net_out)

# 判断是否超过阈值
if [ ${cpu_usage%.*} -gt ${cpu_alert} ]; then
  echo "CPU Usage ${cpu_usage}%, exceeded ${cpu_alert}% alert threshold."
fi

if [ ${mem_usage%.*} -gt ${mem_alert} ]; then
  echo "Memory Usage ${mem_usage}%, exceeded ${mem_alert}% alert threshold."
fi

if [ ${disk_usage%.*} -gt ${disk_alert} ]; then
  echo "Disk Usage ${disk_usage}%, exceeded ${disk_alert}% alert threshold."
fi

if [ ${net_usage} -gt ${net_alert} ]; then
  echo "Network Usage ${net_usage}bps, exceeded ${net_alert}bps alert threshold."
fi
